package com.sxm.yiwei.fragments;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.beans.pinche.HttpHead;
import com.sxm.yiwei.utils.GetUiltsData;
import com.sxm.yiwei.utils.HandleDataUtil;

import org.xutils.ex.HttpException;
import org.xutils.http.RequestParams;


/**
 * Created by KuKa on 2017/10/25.
 */

public abstract class BaseFragment extends Fragment implements GetUiltsData.CallBackUtilsInf{
    private Toast toast;

    @Override
    public void onDestroy() {
        super.onDestroy();
        //RefWatcher refWatcher = ExampleApplication.getRefWatcher(getActivity());
       // refWatcher.watch(this);
    }
    public HttpHead getHeadInfo(){
        SharedPreferences sp=getActivity().getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        //Log.d("getHeadInfo",sp.getString("appid",""));
        return new HttpHead(sp.getString("token",""),sp.getString("appid",""),sp.getString("userId",""),"application/json");
    }
    public void showToast(String text) {
        if (toast == null) {
            toast = Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT);
        } else {
            toast.setText(text);
        }
        toast.show();
    }

    public void httpPost(Context context, String url, JSONObject object, String fromWhere){
        GetUiltsData.xPostMethod(getParamsPost(url,object),context,this,fromWhere);
    }
    public void httpGet(Context context,String url, JSONObject object, String fromWhere){
        GetUiltsData.xGetMethod(getParamsGet(url,object),context,this,fromWhere);
    }
    private RequestParams getParamsPost(String url, JSONObject object){
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",getHeadInfo().getToken());
        params.addHeader("appId",getHeadInfo().getAppId());
        params.addHeader("userId",getHeadInfo().getUserId());
        params.setBodyContent(object.toJSONString());
        return params;
    }
    private RequestParams getParamsGet(String url, JSONObject object){
        RequestParams params = new RequestParams(url);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token",getHeadInfo().getToken());
        params.addHeader("appId",getHeadInfo().getAppId());
        params.addHeader("userId",getHeadInfo().getUserId());
        params.addParameter("params",object.toJSONString());
        return params;
    }

    @Override
    public void onSuccess(String result, String fromWhere) {
        HandleDataUtil.show(fromWhere,result);
        //Log.d(fromWhere,result);
    }

    @Override
    public void onSuccessError(String result, String fromWhere) {
        Log.d(fromWhere,result);
        JSONObject object = JSON.parseObject(result);
        String status = object.getString("status");
        if (!status.equals("1")){
            String failure = object.getString("failure");
            Log.d("failure",failure);
            if (failure.equals("1082")){
                showToast("订单已超时");
            }
        }
    }

    @Override
    public void onError(Throwable ex) {
        showToast(ex.toString());
        Log.d("onError",ex.toString());
        if (ex instanceof HttpException){
            showToast("服务器链接断开");
        }else {

        }
    }
}
